package cmd

import (
	"gitee.com/kristas/booting-go/gen_tool/booctl/logic"
	"gitee.com/kristas/booting-go/gen_tool/booctl/util"
	"github.com/spf13/cobra"
)

var (
	openBrowserUrl string
)

var useCmd = &cobra.Command{
	Use:   "use [plugin]",
	Short: "Use plugin",
}

var useCacheCmd = &cobra.Command{
	Use:   "cache",
	Short: "use built-in cache starter",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseCache()
		cobra.CheckErr(err)
	},
}

var useCacheRedisCmd = &cobra.Command{
	Use:   "redis",
	Short: "use built-in redis starter",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseRedis()
		cobra.CheckErr(err)
	},
}

var useDatasourceCmd = &cobra.Command{
	Use:     "datasource",
	Short:   "use built-in datasource starter",
	Aliases: []string{"data", "ds"},
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseDatasource()
		cobra.CheckErr(err)
	},
}

var useGormCmd = &cobra.Command{
	Use:   "gorm",
	Short: "use gorm plugin",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseGorm()
		cobra.CheckErr(err)
	},
}

var useIrisCmd = &cobra.Command{
	Use:   "iris",
	Short: "use iris web plugin",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseIris()
		cobra.CheckErr(err)
	},
}

var useGinCmd = &cobra.Command{
	Use:   "gin",
	Short: "use gin web plugin",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseGin()
		cobra.CheckErr(err)
	},
}

var useEchoCmd = &cobra.Command{
	Use:   "echo",
	Short: "use echo web plugin",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseEcho()
		cobra.CheckErr(err)
	},
}

var useSecurityCmd = &cobra.Command{
	Use:   "security",
	Short: "use built-in security module",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseSecurity()
		cobra.CheckErr(err)
	},
}

var useSwaggerCmd = &cobra.Command{
	Use:     "swagger",
	Aliases: []string{"swag"},
	Short:   "use swagger plugin",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseSwagger()
		cobra.CheckErr(err)
	},
}

var useWebsocketCmd = &cobra.Command{
	Use:     "websocket",
	Aliases: []string{"ws"},
	Short:   "use websocket plugin",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseWebsocket()
		cobra.CheckErr(err)
	},
}

var useOpenBrowserCmd = &cobra.Command{
	Use:     "open_browser",
	Aliases: []string{"browser", "ob"},
	Short:   "use open browser plugin",
	Run: func(cmd *cobra.Command, args []string) {
		if openBrowserUrl == "" {
			cobra.CheckErr(util.Scan("input page url:", &openBrowserUrl))
		}
		err := logic.UseOpenBrowserPlugin(openBrowserUrl)
		cobra.CheckErr(err)
	},
}

var useFlywayCmd = &cobra.Command{
	Use:   "flyway",
	Short: "use flyway plugin",
	Run: func(cmd *cobra.Command, args []string) {
		err := logic.UseFlywayPlugin()
		cobra.CheckErr(err)
	},
}

func init() {
	useOpenBrowserCmd.Flags().StringVarP(&openBrowserUrl, "url", "u", "", "open browser page link")

	useCmd.AddCommand(useCacheCmd)
	useCmd.AddCommand(useCacheRedisCmd)
	useCmd.AddCommand(useDatasourceCmd)
	useCmd.AddCommand(useGormCmd)
	useCmd.AddCommand(useIrisCmd)
	useCmd.AddCommand(useGinCmd)
	useCmd.AddCommand(useEchoCmd)
	useCmd.AddCommand(useSecurityCmd)
	useCmd.AddCommand(useSwaggerCmd)
	useCmd.AddCommand(useWebsocketCmd)
	useCmd.AddCommand(useOpenBrowserCmd)
	useCmd.AddCommand(useFlywayCmd)

	rootCmd.AddCommand(useCmd)
}
